package org.jeecg.modules.online.cgform.util;

import java.math.BigDecimal;
import java.util.Map;

import org.jeecg.common.constant.DataBaseConstant;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jeecg.modules.online.config.util.DbTableUtil;

import com.alibaba.fastjson.JSONObject;

public class OnlineDbHandler {
	
	public static final String NUM_TYPE_INT = "int";
	// update_begin author:sunjianlei date:20190516 for: 新增判断是否是数值类型另一条件
	public static final String NUM_TYPE_INTEGER = "Integer";
	// update_end author:sunjianlei date:20190516 for: 新增判断是否是数值类型另一条件
	public static final String NUM_TYPE_DOUBLE = "double";
	public static final String NUM_TYPE_DECIMAL = "BigDecimal";
	public static final String NUM_TYPE_BLOB = "Blob";
	public static final String DATE_TYPE = "Date";
	public static final String DATE_TIME_TYPE = "datetime";
	public static final String TIMESTAMP_TYPE = "Timestamp";
	
	
	
	/**
	 * 判断是否是数值类型
	 * @param dbtype
	 * @return
	 */
	public static boolean isNumField(String dbtype) {
		// update_begin author:sunjianlei date:20190516 for: 新增判断是否是数值类型另一条件
		return NUM_TYPE_INT.equals(dbtype)||NUM_TYPE_DOUBLE.equals(dbtype)||NUM_TYPE_DECIMAL.equals(dbtype)||NUM_TYPE_INTEGER.equals(dbtype);
		// update_end author:sunjianlei date:20190516 for: 新增判断是否是数值类型另一条件
	}
	
	/**
	  * 判断是否日期类型
	 * @param dbtype
	 * @return
	 */
	public static boolean isDateField(String dbtype) {
		return DATE_TYPE.equalsIgnoreCase(dbtype) || DATE_TIME_TYPE.equalsIgnoreCase(dbtype) || TIMESTAMP_TYPE.equalsIgnoreCase(dbtype);
	}
	

	/**
	  * 获取mybatis的value表达式 如#{name,jdbcType=VARCHAR}
	 * @param dbType string/int/double/Date/BigDecimal/Text/Blob
	 * @param String
	 * @param pageType date/datetime/text/radio/checkbox/textarea....主要是处理date/datetime
	 * @return
	 */
	public static String initMybatisValueWithJdbcType(String dataBase,OnlCgformField item,JSONObject json,Map<String,Object> params) {
		String dbType = item.getDbType();
		String field = item.getDbFieldName();
		String pageType = item.getFieldShowType();

        //update-begin--Author:scott----  Date:20190924 ----for：online表单编辑，值设置为空问题解决(主要时间、BigDecimal类型，设置值为null)----
		if(json.get(field)==null){
            return "null";
        }
        //update-end--Author:scott----  Date:20190924 ----for：online表单编辑，值设置为空问题解决(主要时间、BigDecimal类型，设置值为null)----

		if(NUM_TYPE_INT.equals(dbType)) {
			params.put(field, json.getIntValue(field));
			return "#{"+field+",jdbcType=INTEGER}";
		}else if(NUM_TYPE_DOUBLE.equals(dbType)) {
			params.put(field, json.getDoubleValue(field));
			return "#{"+field+",jdbcType=DOUBLE}";
		}else if(NUM_TYPE_DECIMAL.equals(dbType)) {
			params.put(field, new BigDecimal(json.getString(field)));
			return "#{"+field+",jdbcType=DECIMAL}";
		}else if(NUM_TYPE_BLOB.equals(dbType)) {
		    //支持 blob字段保存
			params.put(field, json.getString(field)!=null?json.getString(field).getBytes():null);
			return "#{"+field+",jdbcType=BLOB}";
		}else if(DATE_TYPE.equals(dbType)) {
			String fieldVal = json.getString(field);
			if(DataBaseConstant.DB_TYPE_ORACLE.equals(dataBase)) {
				if("date".equals(pageType)) {
					params.put(field,fieldVal.length()>10?fieldVal.substring(0, 10):fieldVal);
					return "to_date(#{"+field+"},'yyyy-MM-dd')";
				}else {
					params.put(field,fieldVal.length()==10?json.getString(field)+" 00:00:00":fieldVal);
					return "to_date(#{"+field+"},'yyyy-MM-dd HH24:mi:ss')";
				}
			}else if(DataBaseConstant.DB_TYPE_POSTGRESQL.equals(dataBase)){
				if("date".equals(pageType)) {
					params.put(field,fieldVal.length()>10?fieldVal.substring(0, 10):fieldVal);
					return "CAST(#{"+field+"} as TIMESTAMP)";
				}else {
					params.put(field,fieldVal.length()==10?json.getString(field)+" 00:00:00":fieldVal);
					return "CAST(#{"+field+"} as TIMESTAMP)";
				}
			}else {
				params.put(field,json.getString(field));
				return "#{"+field+"}";
			}
		}else {
			params.put(field,json.getString(field));
			return "#{"+field+",jdbcType=VARCHAR}";
		}
		
	}

}
